package company.maestro;

import java.io.File;
import java.io.FileNotFoundException;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


/**
 * StudentDetailsActivity displays details about
 * an individual student.
 * @author Maestro
 *
 */
public class StudentDetailsActivity extends Activity 
{
	private ImageView 			studentPicture;
	private TextView			studentName;
	private StudentDBAdapter 	database;
	private Button 				btnCallGuardian;
	private Button 				btnTextGuardian;
	private Button 				btnEmailGuardian;
	private TextView 			lblGuardianName;
	private TextView 			lblGuardianPhoneNumber;
	private TextView 			lblGuardianEmail;
	

	@Override
	/**
	 * Called when the activity is created.
	 */
	public void onCreate(Bundle savedInstanceState) 
	{
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.student_details);
	    
	    studentPicture 			= (ImageView)findViewById(R.id.studentDetailsImage);
	    studentName 			= (TextView)findViewById(R.id.studentDetailsName);
	    btnCallGuardian			= (Button)findViewById(R.id.btnCallGuardian);
	    btnTextGuardian	 		= (Button)findViewById(R.id.btnTextGuardian);
	    btnEmailGuardian 		= (Button)findViewById(R.id.btnEmailGuardian);
	    lblGuardianName 		= (TextView)findViewById(R.id.lblParentGuardianName);
	    lblGuardianPhoneNumber 	= (TextView)findViewById(R.id.lblGuardianPhoneNumber);
	    lblGuardianEmail 		= (TextView)findViewById(R.id.lblGuardianEmail);
	    

	    // The Student was passed to this activity as a Serializable
	    // through "extras" in the Intent.  Here we get the Student
	    // back out of the extras and set the text on the activity
	    // with the Student's information.
	    Student student = null;
	    
	    Bundle extras = getIntent().getExtras(); 
	    if(extras !=null) 
	    {
	    	student   = (Student)extras.getSerializable("STUDENT");
	    }
	    
	    studentName.setText(student.getLastName() + ", " 
	    		+ student.getFirstName());
	    lblGuardianName.setText(student.getGuardianName());
	    lblGuardianPhoneNumber.setText(student.getGuardianPhoneNumber());
	    lblGuardianEmail.setText(student.getGuardianEmail());
	    
	    
	    Bitmap userImage = null;
		try {
			userImage = StudentDBAdapter.getImageForStudent(student.getId());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		studentPicture.setImageBitmap(userImage);
	    
	    
	    
	    // Listener for the Call Guardian Button.  When the button is
	    // pressed, call the phone number on record.
	    btnCallGuardian.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				
				String url = "tel:" + lblGuardianPhoneNumber.getText();
			    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));

				StudentDetailsActivity.this.startActivity(intent);
			
			}
		});
	    
	    
	    // Listener for the Email Guardian.  When the button is
	    // pressed, send an email to the address on record.
	    btnEmailGuardian.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				
				Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                
                emailIntent.setType("plain/text");
           
                // Set the email address to send to
                emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, 
                		new String[]{ (String) lblGuardianEmail.getText() });
         
                // Set the email subject
                emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, 
                		"Test email from Maestro");
         
                // Set the email body
                emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, 
                		"Hello!  This email was sent by Maestro!");
 
                // Send the email
                StudentDetailsActivity.this.startActivity(Intent.createChooser(emailIntent, 
                		"Send mail..."));
			
			}
		});
	    
	    
	}  

}
